﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Entities;
namespace Stationery
{
    public partial class UC_Basket : System.Web.UI.UserControl
    {
        BLL_City bll_City = new BLL_City();
        BLL_Order bll_Order = new BLL_Order();
        BLL_Customer bll_Customer = new BLL_Customer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Set_DataSource_Grid();
                Get_SourceCity();
                ASPxDateEdit1.Date = DateTime.Now.AddDays(1);
            }
            else
            {
                if (Session[Utils.SESSION_CART] != null)
                {
                    ASPxGridView1.DataSource = Session[Utils.SESSION_CART];
                    ASPxGridView1.DataBind();
                }
            }
        }
        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            Int64 intID_PRODUCT = Convert.ToInt64(((LinkButton)sender).CommandArgument.ToString());
            if (Session[Utils.SESSION_CART] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                bll_Order.Delete_Order_FromList(lstOrder, intID_PRODUCT);
                Session[Utils.SESSION_CART] = lstOrder;
            }
            Response.Redirect(Request.RawUrl);
        }
        private void Get_SourceCity()
        {
            ASPxComboBoxCity.DataSource = bll_City.Get_City_All();
            ASPxComboBoxCity.DataBind();
            ASPxComboBoxCity.Text = "Hà Nội";
            ASPxComboBoxCity.Value = "Hà Nội";
        }
        private void Set_DataSource_Grid()
        {
            if (Session[Utils.SESSION_CART] != null)
            {
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                ASPxGridView1.DataSource = lstOrder;
                ASPxGridView1.DataBind();
            }
        }

        protected void ASPxButtonUpdate_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART] != null)
            {
                UpdateOrder();
                Set_DataSource_Grid();
            }
        }
        private void UpdateOrder()
        {
            List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART];
            for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
            {
                DevExpress.Web.ASPxEditors.ASPxSpinEdit sb = ASPxGridView1.FindRowCellTemplateControl(i, ASPxGridView1.Columns["QUANTITY"] as DevExpress.Web.ASPxGridView.GridViewDataColumn, "ASPxSpinEdit1") as DevExpress.Web.ASPxEditors.ASPxSpinEdit;
                Int64 intID_PRODUCT = Convert.ToInt64(ASPxGridView1.GetRowValues(i, "ID_PRODUCT"));
                Int64 intQUANTITY = Convert.ToInt64(sb.Value);
                for (int j = 0; j < lstOrder.Count; j++)
                {
                    if (intID_PRODUCT == lstOrder[j].ID_PRODUCT)
                    {
                        lstOrder[j].QUANTITY = intQUANTITY;
                        UpdatePriceValue(lstOrder[j]);
                        break;
                    }
                }
            }
            Session[Utils.SESSION_CART] = lstOrder;
        }
        protected void ASPxButtonOrder_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_CART] != null)
            {
                UpdateOrder();
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                string strOrderCode = bll_Order.Get_Order_Code();
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    lstOrder[i].FULLNAME = ASPxTextBoxFullName.Text;
                    lstOrder[i].ADDRESS = ASPxTextBoxAddress.Text;
                    lstOrder[i].CITY = ASPxComboBoxCity.Text;
                    lstOrder[i].PHONE = ASPxTextBoxPhone.Text;
                    lstOrder[i].EMAIL = ASPxTextBoxEmail.Text;
                    lstOrder[i].DESCRIPTION = ASPxMemoNote.Text;
                    lstOrder[i].STATUS = "P";//pending
                    lstOrder[i].ORDER_DATE = DateTime.Now;
                    lstOrder[i].RECEIVE_DATE = ASPxDateEdit1.Date;
                    lstOrder[i].ORDER_CODE = strOrderCode;
                }
                bll_Order.InsertUpdate_ListOrder(lstOrder);
                Session[Utils.SESSION_CART] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Đặt hàng thành công. Chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất.');location.href = 'Default.aspx';", true);
            }
            else
            {
                MessageBox.Show("Phải có ít nhất 1 sản phẩm trong giỏ hàng");
            }
        }

        protected void ASPxButtonContinue_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.home);
        }

        private void UpdatePriceValue(Order pOrder)
        {
            pOrder.VALUE = pOrder.PRICE * pOrder.QUANTITY;
            pOrder.TOTAL = pOrder.VALUE + pOrder.VALUE * pOrder.TAX;
        }

        protected void ASPxTextBoxEmail_ValueChanged(object sender, EventArgs e)
        {
            string strEmail = ASPxTextBoxEmail.Text.Trim();
            string strPhone = ASPxTextBoxPhone.Text.Trim();
            UpdateInforCustomer(strEmail, strPhone);

        }
        private void UpdateInforCustomer(string pEmail, string pPhone)
        {
            List<Customer> lstCustomer = bll_Customer.Lst_Customer(bll_Customer.Get_Customer_Email_Phone(pEmail, pPhone));
            if (lstCustomer.Count > 0)
            {
                Customer customer = lstCustomer[0];
                if (customer != null)
                {
                    ASPxTextBoxPhone.Text = customer.PHONE;
                    ASPxTextBoxEmail.Text = customer.EMAIL;
                    ASPxTextBoxAddress.Text = customer.ADDRESS;
                    ASPxTextBoxFullName.Text = customer.FULLNAME;
                    //ASPxMemoNote.Text = customer.DESCRIPTION;
                    ASPxComboBoxCity.Text = customer.CITY;
                }
                else
                {

                }
            }
        }

        //protected void ASPxTextBoxPhone_ValueChanged(object sender, EventArgs e)
        //{
        //    string strEmail = ASPxTextBoxEmail.Text.Trim();
        //    string strPhone = ASPxTextBoxPhone.Text.Trim();
        //    UpdateInforCustomer(strEmail, strPhone);
        //}
        protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string strParam = e.Parameters.ToString();
            string[] lstString = strParam.Split(',');
            int intID_PRODUCT = Convert.ToInt32(lstString[0]);
            int intNUMBER = Convert.ToInt32(lstString[1]);
            if (Session[Utils.SESSION_CART] != null)
            {
                List<Order> lstOrder = new List<Order>();
                lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                foreach (Order order in lstOrder)
                {
                    if (order.ID_PRODUCT == intID_PRODUCT)
                    {
                        order.QUANTITY = intNUMBER;
                        UpdatePriceValue(order);
                        break;
                    }
                }
                Session[Utils.SESSION_CART] = lstOrder;
                ASPxGridView1.DataSource = Session[Utils.SESSION_CART];
                ASPxGridView1.DataBind();
            }
        }

        protected void ASPxCallbackPanel1_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            switch (e.Parameter)
            {
                case "0":
                    UpdateInforCustomer("", ASPxTextBoxPhone.Text.Trim());
                    break;
                case "1":
                    UpdateInforCustomer(ASPxTextBoxEmail.Text.Trim(), "");
                    break;
                default:
                    break;
            }
        }
    }
}
